<?php
namespace app\circle\controller;
use app\circle\logic\CircleCommentLogic;

/**
 * 工作圈评论api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/26
 * Time: 09:56
 */
class CircleComment extends Base {

    public function __construct(){
        parent::__construct();
        $this->cc_logic = new CircleCommentLogic();
    }

    /**
     * 发布工作圈评论
     * @return \think\response\Json
     */
    public function ajax_send_wcc(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->cc_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查发布工作圈评论规则
        $r = $this->cc_logic->check_send_wcc_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $wcm_detail = $r['data']['wcm_detail'];

        $wcc_data = [];
        $wcc_data['openid'] = $u_detail['openid'];
        $wcc_data['wcmid'] = $wcm_detail['id'];
        $wcc_data['type'] = 1;
        $wcc_data['content'] = trim($params['content']);
        $wccid = model('work_circle_comment')->insertGetId($wcc_data);
        if($wccid){
            //更新评论及被评论工作圈积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_cc_integral($wccid);

            return json($this->cc_logic->get_response(1,'评论成功'));
        }
        return json($this->cc_logic->get_response(0,'评论失败'));
    }

    /**
     * 发布工作圈点赞
     * @return \think\response\Json
     */
    public function ajax_send_wct(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->cc_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查发布工作圈点赞规则
        $r = $this->cc_logic->check_send_wct_rule($u_detail['openid'],$params);
        if(!$r['code']){
            return json($r);
        }
        $wcm_detail = $r['data']['wcm_detail'];

        $wcc_data = [];
        $wcc_data['openid'] = $u_detail['openid'];
        $wcc_data['wcmid'] = $wcm_detail['id'];
        $wcc_data['type'] = 2;
        $wccid = model('work_circle_comment')->insertGetId($wcc_data);
        if($wccid){
            //更新点赞及被点赞工作圈积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_ct_integral($wccid);

            return json($this->cc_logic->get_response(1,'点赞成功'));
        }
        return json($this->cc_logic->get_response(0,'点赞失败'));
    }
}
